/*
 * @LastEditors: yuSeen
 * @LastEditTime: 2022-01-11 12:36:32
 */
import { useConcent } from "concent";
import { useLocalStorage } from "@/hooks/localStorage/useLocalStorage";

const { setStorageItem } = useLocalStorage();

const setup = (ctx: any) => {
  return {
    setToken: (token: string) => ctx.moduleReducer.setToken(token),
    updateUserInfo: () => ctx.moduleReducer.updateUserInfo(),
  };
};

export const useUser = () => {
  const {
    state,
    moduleComputed: mcu,
    settings,
  } = useConcent({ module: "user", setup });

  //   设置token
  const setToken = (token: string) => {
    settings.setToken(token);
    settings.updateUserInfo();
    setStorageItem("token", token);
  };

  //   更新用户信息
  const updateUserInfo = () => {
    settings.updateUserInfo();
  };

  return {
    userInfo: state.userInfo,
    isLogin: mcu.isLogin,
    token: state.token,
    setToken,
    updateUserInfo,
  };
};
